package com.edu.stress.test.service;

import com.edu.stress.test.constant.Constant;
import com.edu.stress.test.util.Codec2;
import com.edu.stress.test.util.HttpUtil;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;

import java.util.Date;

public class RegisterService {
    private static final Logger logger = Logger.getLogger(RegisterService.class);

    private String vin;
    private String sign;
    private Long timestamp;
    private String requestUrl;

    private String requestBody;
    private String response;

    private String deviceId;
    private String secret;

    public RegisterService(String tempVin) {
        vin = tempVin;
        Date date = new Date();
        timestamp =date.getTime() / 1000;
        sign = Codec2.hexString(Constant.TOKEN, tempVin + timestamp);
        requestUrl = Constant.DOMAIN + "/register";
    }

    public boolean register() {
        boolean resStatus = false;

        requestBody = "{\"vin\": \"" + vin + "\",\"sdkVersion\": \"v1.0\",\"sign\": \"" + sign +
                "\",\"timestamp\": \"" + timestamp +
                "\",\"ecus\": [" +
                "{\"ecuPartNum\": \"per111222\",\"ecuDid\": \"perD0817\",\"ecuSVer\": \"123123\",\"ecuHVer\": \"test001\",\"ecuHsn\": \"test001sn\",\"tieroneName\": \"0810001\"}]}";


        try {
            response = HttpUtil.postJson(requestUrl, requestBody);
            if (response != null && !response.trim().equals("")) {
                JSONObject rsjson = JSONObject.fromObject(response);
                if (rsjson.getString("status").equals("1000")) {
                    String dt = rsjson.getString("data").toString();
                    JSONObject jsonOb = JSONObject.fromObject(dt);
                    deviceId = jsonOb.get("deviceId").toString();
                    secret = jsonOb.get("secret").toString();
                    resStatus = true;
                }else {
                    logger.error("注册status:" + rsjson.getString("status"));
                    logger.error("注册msg:" + rsjson.getString("msg"));
                }
            }else {
                logger.error("注册服务调用异常，返回值为空");
            }
        } catch (Exception e) {
            logger.error("注册服务调用未知异常");
            e.printStackTrace();
        }

        logger.info("注册URL:" + requestUrl);
        logger.info("注册请求:" + requestBody);
        logger.info("注册响应:" + response);

        return resStatus;
    }

    public static Logger getLogger() {
        return logger;
    }

    public String getVin() {
        return vin;
    }

    public void setVin(String vin) {
        this.vin = vin;
    }

    public String getSign() {
        return sign;
    }

    public void setSign(String sign) {
        this.sign = sign;
    }

    public Long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(Long timestamp) {
        this.timestamp = timestamp;
    }

    public String getRequestUrl() {
        return requestUrl;
    }

    public void setRequestUrl(String requestUrl) {
        this.requestUrl = requestUrl;
    }

    public String getRequestBody() {
        return requestBody;
    }

    public void setRequestBody(String requestBody) {
        this.requestBody = requestBody;
    }

    public String getResponse() {
        return response;
    }

    public void setResponse(String response) {
        this.response = response;
    }

    public String getDeviceId() {
        return deviceId;
    }

    public void setDeviceId(String deviceId) {
        this.deviceId = deviceId;
    }

    public String getSecret() {
        return secret;
    }

    public void setSecret(String secret) {
        this.secret = secret;
    }
}
